﻿using ZeroSum.App.Extensions;
using ZeroSum.App.Middlewares;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.DependencyInjection.Interfaces;
using ZeroSum.Extend.Cors;
using ZeroSum.Extend.EFCore.MySql;
using ZeroSum.Extend.Mapper;
using ZeroSum.Extend.Swagger;

namespace ZeroBlog.Api
{
    /// <summary>
    ///     Identity层初始化注册，要早于AspNetCore
    ///     晚于Domain, Infrastructure, Application
    /// </summary>
    [StartupOrder(1001)]
    public class Startup : IAppStartup
    {
        public void ConfigurationServices(IServiceCollection services, IConfiguration configuration)
        {
            // 注册授权相关服务的实现

            services.AddDistributedMemoryCache();
            services.AddAutoMapper();
            services.AddHttpContextAccessor();
            services.AddControllers().AddFriendlyResponse();
            services.AddCorsSupported(configuration);

            services.AddEFCore(configuration);
            services.AddSwaggerDoc(configuration);

        }

        public void Configuration(IApplicationBuilder app)
        {
            // 全局异常处理
            app.EnableExceptionHandler();

            // 自动提交数据库改动
            app.EnableAutoSave();
            app.UseCorsSupported();

            app.UseSwaggerDoc();
            app.UseAutoMapper();
            app.UseRouting();

            // app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}